package com.example.hemin.fnb.ui.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hemin.fnb.R;
import com.example.hemin.fnb.ui.base.BaseMvpActivity;
import com.example.hemin.fnb.ui.util.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopAddressMessage extends BaseMvpActivity {
    @BindView(R.id.lay_back)
    LinearLayout layBack;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.titl2)
    TextView titl2;
    @BindView(R.id.text)
    EditText text;
    @BindView(R.id.text2)
    EditText text2;
    @BindView(R.id.text3)
    EditText text3;
    @BindView(R.id.text5)
    TextView text5;
    @BindView(R.id.image)
    ImageView image;
    private int status = 0;

    @Override
    public int getLayoutId() {
        return R.layout.shop_address_message;
    }

    @Override
    public void initView() {
        title.setText("编辑收货地址");
        titl2.setTextColor(this.getResources().getColor(R.color.F99707));
        titl2.setVisibility(View.VISIBLE);
        Intent intent = getIntent();
        int isStatus = intent.getIntExtra("isStatus",0);
        if(isStatus == 0){

        }else if(isStatus == 1){
            titl2.setText("保存");
            String name = intent.getStringExtra("name");
            String phone = intent.getStringExtra("phone");
            String address = intent.getStringExtra("address");
            int status = intent.getIntExtra("status",0);
            if(status == 1){
                image.setImageResource(R.mipmap.shop_adress_buttons);
            }else if(status == 0){
                image.setImageResource(R.mipmap.shop_address_button);
            }
            text.setText(name);
            text2.setText(phone);
            text3.setText(address);

        }

    }
    @OnClick({R.id.lay_back, R.id.titl2, R.id.text5,R.id.c1})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lay_back:
                finish();
                break;
            case R.id.titl2:
                if(getUser() != null && Utils.isPhoneNumber(getPhone()) && getAddress()!= null){
                    SharedPreferences.Editor sp = getSharedPreferences("userAddress",MODE_PRIVATE).edit();
                     sp.putString("name",getUser());
                     sp.putString("phone",getPhone());
                     sp.putString("address",getAddress());
                     sp.putInt("status",status);
                     sp.commit();
                    Intent intent = new Intent();
                    setResult(1,intent);
                    finish();
                }else {
                    Toast.makeText(getApplicationContext(),"输入完整",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.text5:
                break;
            case R.id.c1:
                  if(status == 0){
                      status = 1;
                      image.setImageResource(R.mipmap.shop_adress_buttons);
                  }else if(status == 1){
                      status = 0;
                      image.setImageResource(R.mipmap.shop_address_button);
                  }
                break;
        }
    }
    private String getUser(){
        return  text.getText().toString().trim();
    }
    private String getPhone(){
        return  text2.getText().toString().trim();
    }
    private String getAddress(){
        return text3.getText().toString().trim();
    }
    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


}
